<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
require('echarts/theme/macarons') // echarts theme
import resize from '@/components/Charts/mixins/resize'
export default {
  name: 'AuthSchemePie',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '260px'
    }
  },
  data() {
    return {
      chart: null,
      value: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.getData()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, 'macarons')
      const initOption = {
        title: {
          show: true,
          text: '过去一周认证置信度',
          textStyle: {
            color: 'black'
          }
        },
        tooltip: {
          trigger: 'item',
          backgroundColor: 'rgba(255,255,255,0.8)', // 设置背景图片 rgba格式
          color: 'black',
          borderWidth: '1', // 边框宽度设置1
          borderColor: 'gray', // 设置边框颜色
          textStyle: {
            color: 'black' // 设置文字颜色
          },
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          data: ['单口令', '单短信', '动态令牌', 'Ukey', '短信验证码结合口令', '占位1', '占位2'],
          left: '80%'
        },
        series: [
          {
            name: '认证生物特征类型占比',
            type: 'pie',
            data: [
              { name: '单口令' },
              { name: '单短信' },
              { name: '动态令牌' },
              { name: 'Ukey' },
              { name: '短信验证码结合口令' },
              { name: '占位1' },
              { name: '占位2' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2000
          }
        ]
      }
      this.chart.setOption(initOption)
    },
    getData() {
      this.value = [
        { value: 12341, name: '单口令' },
        { value: 12342, name: '单短信' },
        { value: 12345, name: '动态令牌' },
        { value: 12145, name: 'Ukey' },
        { value: 12345, name: '短信验证码结合口令' },
        { value: 18952, name: '占位1' },
        { value: 15893, name: '占位2' }
      ]
      this.updateChart()
    },
    updateChart() {
      const updateOption = {
        series: [
          { data: this.value }
        ]
      }
      this.chart.setOption(updateOption)
    }
  }
}
</script>

<style scoped>

</style>
